#include <QtGui>
#include "mastermeasure.h"
#include "../wrapper.h"
#include "../core/ctrl.h"
#include "../core/ctrlthread.h"

MasterMeasure::MasterMeasure(const QString &item, QWidget *parent)
	: QWidget(parent)
{
	ui_.setupUi(this);

	ui_.label_->setText(item);

	if(item.contains("HUE") || item.contains("POLH")){
		master_kind_ = 0;
	}else if(item.contains("VL")){
		master_kind_ = 1;
	}else if(item.contains("DLM")){
		master_kind_ = 2;
	}

	QString channel = item.right(2);
	channel = channel.left(1);
	chn_no_ = channel.toInt();
	
	ui_.tablewidget_->setRowCount(14);
	ui_.tablewidget_->setColumnCount(1);
	ui_.tablewidget_->horizontalHeader()->setVisible(false);
	QStringList table_headers[3];
	table_headers[0] << tr("TEMP(deg)") << tr("VALUE1") << tr("VALUE2") 
			 << tr("V2/V1") << tr("C_LOG") << tr("G_NUM") << tr("L_NUM")
			 << tr("RANGE1") << tr("GAIN1") << tr("RANGE2") << tr("GAIN2");
	table_headers[1] << tr("MAX.VL") << tr("MIN.VL") << tr("STP.VL") << tr("RANGE")
			 << tr("GAIN") << tr("VL1.AD") << tr("VL2.AD") << tr("CO.a1")
			 << tr("CO.a2") << tr("CO.b") << tr("CO.d");
	table_headers[2] << tr("Rs") << tr("Gs") << tr("Bs") << tr("CoefR") << tr("CoefG")
			 << tr("CoefB") << tr("Cal x") << tr("Cal y") << tr("RANGE1")
			 << tr("GAIN1") << tr("RANGE2") << tr("GAIN2") << tr("RANGE3")
			 << tr("GAIN3");
	ui_.tablewidget_->setVerticalHeaderLabels(table_headers[master_kind_]);
	if(master_kind_ != 2){
		for(int i = 11; i < 14; ++i){
			ui_.tablewidget_->setRowHidden(i, true);
		}
	}
	QLabel *label;
	for(int row_index = 0; row_index < 14; ++row_index){
		label = new QLabel;
		label->setAlignment(Qt::AlignCenter);
		ui_.tablewidget_->setCellWidget(row_index, 0, label);
	}

	// set font size
	QFont font = this->font();
	font.setPixelSize(font.pointSize() * 4 / 3);
	setFont(font);

	setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
	showMaximized();

	Ctrl *ctrl = Ctrl::getInstance();
	CtrlThread *thread = ctrl->getCtrlThread();
	disconnect(thread, SIGNAL(measDataReady()), 0, 0);
	connect(thread, SIGNAL(measDataReady()), this, SLOT(ShowMasterData()));

	connect(ui_.back_button_, SIGNAL(clicked()), this, SLOT(ButtonClicked()));
	connect(ui_.start_button_, SIGNAL(clicked()), this, SLOT(ButtonClicked()));
	connect(ui_.save_button_, SIGNAL(clicked()), this, SLOT(ButtonClicked()));
}

MasterMeasure::~MasterMeasure()
{
	delete ui_.label_;
	delete ui_.back_button_;
	delete ui_.start_button_;
	delete ui_.save_button_;
	delete ui_.tablewidget_;
}



void MasterMeasure::ButtonClicked()
{
	QPushButton *button = qobject_cast<QPushButton *>(sender());
	if(button == ui_.back_button_){
		FormShouldReturn();
	}else if(button == ui_.start_button_){
		StartMasterMeasure();
	}else if(button == ui_.save_button_){
		SaveMasterResult();
	}
}



void MasterMeasure::FormShouldReturn()
{
	delete this;
}
void MasterMeasure::SaveMasterResult()
{
	CopyProgramToMeasProg();
}
void MasterMeasure::StartMasterMeasure()
{
	Ctrl* p = Ctrl::getInstance();
	p->cpyProgramToShm();

	// for test only
	program_s *program = getTestProgPtr();
	qDebug() << " ============= program of src_ok";
	qDebug() << program->meastype;
	qDebug() << program->kinds[0].ofstcancel;
	qDebug() << program->kinds[0].cooldown;
	qDebug() << program->kinds[0].hue_mlums[0];
	// for test only

	p->startCtrlThread();
}


void MasterMeasure::ShowMasterData()
{
	switch(master_kind_){
		case 0:
			ShowHueResult();
			break;
		case 1:
			ShowVlResult();
			break;
		case 2:
			ShowDlmResult();
			break;
		default:
			break;
	}
}

void MasterMeasure::ShowVlResult()
{
	Ctrl* pCtrl = Ctrl::getInstance();
	program_s* p = pCtrl->getProgShmPtr();
	Vl_cfg* vl = &p->kinds[0].vl_cfg[chn_no_-1];

	QLabel* label;
	QTableWidget *table = ui_.tablewidget_;
	label = qobject_cast<QLabel*>(table->cellWidget(5, 0));
	label->setText(QString::number(vl->v1));
	label = qobject_cast<QLabel*>(table->cellWidget(6, 0));
	label->setText(QString::number(vl->v2));
	label = qobject_cast<QLabel*>(table->cellWidget(7, 0));
	label->setText(QString::number(vl->a1));
	label = qobject_cast<QLabel*>(table->cellWidget(8, 0));
	label->setText(QString::number(vl->a2));
	label = qobject_cast<QLabel*>(table->cellWidget(9, 0));
	label->setText(QString::number(vl->b));
	label = qobject_cast<QLabel*>(table->cellWidget(10, 0));
	label->setText(QString::number(vl->d));
}
void MasterMeasure::ShowHueResult()
{
	Ctrl *ctrl = Ctrl::getInstance();
	program_s *program = ctrl->getProgShmPtr();
	Hue_cfg *hue = &program->kinds[0].hue_cfg[chn_no_-1];

	QLabel *label;
	label = qobject_cast<QLabel *>(ui_.tablewidget_->cellWidget(0, 0));
	label->setText(QString::number(hue->Mastertemp));
	label = qobject_cast<QLabel *>(ui_.tablewidget_->cellWidget(1, 0));
	label->setText(QString::number(hue->v1));
	label = qobject_cast<QLabel *>(ui_.tablewidget_->cellWidget(2, 0));
	label->setText(QString::number(hue->v2));
	label = qobject_cast<QLabel *>(ui_.tablewidget_->cellWidget(3, 0));
//	label->setText();
	label = qobject_cast<QLabel *>(ui_.tablewidget_->cellWidget(4, 0));
	label->setText(QString::number(hue->C_log));
	label = qobject_cast<QLabel *>(ui_.tablewidget_->cellWidget(5, 0));
	label->setText(QString::number(hue->G_lum));
	label = qobject_cast<QLabel *>(ui_.tablewidget_->cellWidget(6, 0));
	label->setText(QString::number(hue->N_lum));
	label = qobject_cast<QLabel *>(ui_.tablewidget_->cellWidget(7, 0));
//	label->setText();
	label = qobject_cast<QLabel *>(ui_.tablewidget_->cellWidget(8, 0));
//	label->setText();
	label = qobject_cast<QLabel *>(ui_.tablewidget_->cellWidget(9, 0));
//	label->setText();
	label = qobject_cast<QLabel *>(ui_.tablewidget_->cellWidget(10, 0));
//	label->setText();
}
void MasterMeasure::ShowDlmResult()
{
	Ctrl* pCtrl = Ctrl::getInstance();
	program_s* p = pCtrl->getProgShmPtr();
	Dlm_cfg* dlm = &p->kinds[0].dlm_cfg[chn_no_-1];

	QLabel* label;
	QTableWidget *table = ui_.tablewidget_;
	label = qobject_cast<QLabel*>(table->cellWidget(3, 0));
	label->setText(QString::number(dlm->CoefR));
	label = qobject_cast<QLabel*>(table->cellWidget(4, 0));
	label->setText(QString::number(dlm->CoefG));
	label = qobject_cast<QLabel*>(table->cellWidget(5, 0));
	label->setText(QString::number(dlm->CoefB));
}
//void MasterMeasure::initVlTable()
//{
//	QStringList table_header;
//	table_header << tr("MAX.VL") << tr("MIN.VL") << tr("STP.VL") << tr("RANGE")
//			 << tr("GAIN") << tr("VL1.AD") << tr("VL2.AD") << tr("CO.a1")
//			 << tr("CO.a2") << tr("CO.b") << tr("CO.d");
//	
//}
//void MasterMeasure::initHueTable()
//{
//	QStringList table_header;
//	table_header << tr("TEMP(deg)") << tr("VALUE1") << tr("VALUE2") 
//			 << tr("V2/V1") << tr("C_LOG") << tr("G_NUM") << tr("L_NUM")
//			 << tr("RANGE1") << tr("GAIN1") << tr("RANGE2") << tr("GAIN2");
//}
//void MasterMeasure::initDlmTable()
//{
//	QStringList table_header;
//	table_header << tr("Rs") << tr("Gs") << tr("Bs") << tr("CoefR") << tr("CoefG")
//			 << tr("CoefB") << tr("Cal x") << tr("Cal y") << tr("RANGE1")
//			 << tr("GAIN1") << tr("RANGE2") << tr("GAIN2") << tr("RANGE3")
//			 << tr("GAIN3");
//	
//}
